package com.alib.springcloud.sentinelResource;

import com.alib.springcloud.entities.CommonResult;
import com.alib.springcloud.entities.Payment;
import com.alib.springcloud.sentinelResourceBlock.BlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AllController {
    /**
     * 按照资源名字注册进入 sentinel
     * 1、value = "byResource" 填了之后，会在 sentinel 仪表盘显示
     * 2、临时的，若8701服务器关闭，sentinel 仪表盘也随之消失
     * */
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource()
    {
        return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020,"serial001"));
    }
    public CommonResult handleException(BlockException exception)
    {
        return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
    }

    /**
     * 按 url:  /rateLimit/byUrl 注册进入 sentinel
     *
     * **/
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按url限流测试OK",new Payment(3030,"serial002"));
    }


    /**
     * 自定义返回失败提示类
     * */
    @GetMapping("/rateLimit/ce1")
    @SentinelResource(value = "ce1")
    public CommonResult ce1()
    {
        return new CommonResult(200,"ce1",new Payment(2222,"ce1"));
    }

    // 这个较常用，使用 全局的失败返回类，且是 自定义的。
    @GetMapping("/rateLimit/ce2")
    @SentinelResource(value = "ce2", blockHandlerClass = BlockHandler.class, blockHandler = "handleException")
    public CommonResult ce2()
    {
        return new CommonResult(200,"ce2",new Payment(3333,"ce2"));
    }

}

